using System;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Templates;
using AvaloniaUiKit.SampleApp.ViewModels.Pages;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace AvaloniaUiKit.SampleApp.Controls
{
    public class ViewLocator : IDataTemplate
    {
        public Control Build(object param)
        {
            if (param is null)
                return new TextBlock { Text = "没有选择视图" };
      
            var name = param.GetType().FullName!.Replace("ViewModels", "Views").Replace("ViewModel", ""); ;
            var type = Type.GetType(name);

            if (type != null)
            {
                return (Control)Activator.CreateInstance(type)!;
            }
            
            return new TextBlock { Text = $"未找到视图: {name}" };
        }

        public bool Match(object data)
        {
            return data is IPageViewModel;
        }
    }
}